﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Threading;
using System.Xml.Serialization;
using System.IO;
using Terminal.Advanced_AEUC_settings_controls.Scripting;

namespace OMXParamsConfig
{
    static class Program
    {
        private static Semaphore instanceCreated;
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            try
            {
                instanceCreated = Semaphore.OpenExisting("OMXConfigPCToolInstanceCount");
                MessageBox.Show("Multiple instances of this application are not allowed!");
            }
            catch
            {
                instanceCreated = new Semaphore(0, 1, "OMXConfigPCToolInstanceCount");
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);

                bool scrit_srv = false;

                if (args.Length > 0)
                {
                    for (int i = 0; i < args.Length; i++)
                    {
                        switch (args[i])
                        {
                            case "-script_srv":
                                scrit_srv = true;
                                break;
                            case "-def_comm":
                                if (++i < args.Length)
                                {
                                    string def_comm = args[i];
                                    CommLink.Properties.Settings.Default.Default_communication_type = def_comm;
                                    CommLink.Properties.Settings.Default.Save();
                                }
                                else
                                {
                                    MessageBox.Show("Incorrect usage: -def_comm <comm_type> expects a valid communication type");
                                    i = args.Length;
                                }
                                break;
                            case "-load_pjt":
                                if(++i < args.Length)
                                {
                                    FileInfo loadPjt = new FileInfo(args[i]);
                                    if (loadPjt.Exists)
                                    {
                                        Properties.Settings.Default.LastLoadedComponent = loadPjt.FullName;
                                        Properties.Settings.Default.Save();
                                    }
                                    else
                                    {
                                        MessageBox.Show("File " + loadPjt.FullName + " doesn't exist!");
                                        i = args.Length;
                                    }
                                }
                                else
                                    MessageBox.Show("Incorrect usage: -load_pjt <file_path> expects a valid file path");
                                break;
                            case "-script_dir":
                                if (++i < args.Length)
                                {
                                    DirectoryInfo dirInf = new DirectoryInfo(args[i]);
                                    if (dirInf.Exists)
                                    {
                                        ScriptModule.Properties.Settings.Default.FastScriptMenuFolder = dirInf.FullName;
                                        ScriptModule.Properties.Settings.Default.Save();
                                    }
                                    else
                                    {
                                        MessageBox.Show("Direcotry " + dirInf.FullName + " doesn't exist!");
                                        i = args.Length;
                                    }
                                }
                                else
                                    MessageBox.Show("Incorrect usage: -script_dir <fdirectory_path> expects a valid directory path");
                                break;
                            default:
                                MessageBox.Show("Unknown command. Usage tip:\n" + 
                                                    "-script_srv places OMX_AL in script server mode.\n" +
                                                    "-script_dir <fdirectory_path> sets the default scripts directory for relative script calls.\n" +
                                                    "-load_pjt <file_path> starts OMX_AL and loads specified OMX project.\n" +
                                                    "-def_comm <comm_type> selects the default communication media");
                                i = args.Length;
                                break;
                        }
                    }
                }

                Application.Run(new OMXParamForm(scrit_srv));
            }
        }
    }
}
